import personal from '../../api/personal.js'
import toast from '../../utils/toast.js'
import date from '../../utils/date.js'
import login from '../../api/login.js'
const values = {
	old_pwd: '',
	new_pwd: '',
	query_pwd: '',
}
const bankValues = {
	username: '',
	bank_card: '',
	bank_name: '',
	bank_mobile: '',
}
const transaction={
	old_pwd:'',
	new_pwd:'',
}
const state = {
	transaction:JSON.parse(JSON.stringify(transaction)),
	litigation:'',
	values: JSON.parse(JSON.stringify(values)),
	fields: [{
			label: '旧密码',
			prop: 'old_pwd',
			type: 'password',
			labelWidth: '160',
			placeholder: '请输入旧密码',
		},
		{
			label: '新密码',
			prop: 'new_pwd',
			type: 'password',
			labelWidth: '160',
			placeholder: '请输入新密码',
		},
		{
			label: '确认密码',
			prop: 'query_pwd',
			type: 'password',
			labelWidth: '160',
			placeholder: '请输入确认密码',
		},
	],
	rules: {
		old_pwd: [{
			required: true,
			message: '请输入旧密码',
			trigger: 'blur,change'
		}, ],
		new_pwd: [{
			required: true,
			message: '请输入新密码',
			trigger: 'blur,change'
		}, ],
		query_pwd: [{
			required: true,
			message: '请输入确认密码',
			trigger: 'blur,change'
		}, ]
	},
	bank: {
		name: '',
		icon: '',
		background: '',
		bank_card: ''
	},
	card: [{
			name: '农业银行',
			icon: require('../../static/images/Agriculture.png'),
			background: '#009C96',
			color: '#80CDCA',
		},
		{
			name: '中国银行',
			icon: require('../../static/images/China.png'),
			background: '#B81C22',
			color: '#DB8E91',
		},
		{
			name: '工商银行',
			icon: require('../../static/images/business.png'),
			background: '#D62629',
			color: '#EA9394',
		},
		{
			name: '建设银行',
			icon: require('../../static/images/build.png'),
			background: ' #003A90',
			color: '#809DC7',
		},
		{
			name: '交通银行',
			icon: require('../../static/images/traffic.png'),
			background: '#00367A',
			color: '#809BBD',
		},
		{
			name: '招商银行',
			icon: require('../../static/images/zhaoshang.png'),
			background: '#A61F23',
			color: '#D28F91',
		},
		{
			name: '邮政储蓄银行',
			icon: require('../../static/images/Post.png'),
			background: ' #007E3E',
			color: '#80BF9F',
		},
		{
			name: '平安银行',
			icon: require('../../static/images/pinan.png'),
			background: ' #DE5411',
			color: '#EEAA88',
		},
		{
			name: '光大银行',
			icon: require('../../static/images/guangda.png'),
			background: ' #6A1684',
			color: '#B58BC1',
		},
		{
			name: '华夏银行',
			icon: require('../../static/images/Huaxia.png'),
			background: '#E2301F',
			color: '#F0988E',
		},
		{
			name: '兴业银行',
			icon: require('../../static/images/xinye.png'),
			background: '#004187',
			color: '#80A0C3',
		},
		{
			name: '中信银行',
			icon: require('../../static/images/zhongxin.png'),
			background: '#D7000F',
			color: '#EB8087',
		},
		{
			name: '民生银行',
			icon: require('../../static/images/mingshneg.png'),
			background: '#0070BF',
			color: '#80B8DF',
		},
		{
			name: '浙商银行',
			icon: require('../../static/images/zheshang.png'),
			background: '#E60012',
			color: '#F28089',
		},
		{
			name: '其他银行',
			icon: require('../../static/images/qita.png'),
			background: '#E24D49',
			color: '#F0A6A4',
		},
	],
	bankValues: JSON.parse(JSON.stringify(bankValues)),
	bankFields: [{
			label: '姓名',
			prop: 'username',
			type: 'input',
			labelWidth: '180',
			placeholder: '请输入姓名',
		},
		{
			label: '银行卡号',
			prop: 'bank_card',
			type: 'input',
			labelWidth: '180',
			placeholder: '请输入银行卡号',
		},
		{
			label: '所属银行',
			prop: 'bank_name',
			type: 'select',
			labelWidth: '180',
			show:false,
			placeholder: '请选择所属银行',
		},
		{
			label: '预留手机号',
			prop: 'bank_mobile',
			type: 'input',
			labelWidth: '180',
			placeholder: '请输入预留手机号',
		},
	],
	bankRules: {
		username: [{
			required: true,
			message: '请输入姓名',
			trigger: 'blur,change'
		}, ],
		bank_card: [{
			required: true,
			message: '请输入银行卡号',
			trigger: 'blur,change'
		},
		{
			pattern:  /^([1-9]{1})(\d{14}|\d{18})$/g,
			// 正则检验前先将值转为字符串
			transform(value) {
				return String(value);
			},
			message: '请输入正确的银行卡号'
		}, ],
		bank_name: [{
			required: true,
			message: '请选择所属银行',
			trigger: 'blur,change'
		}, ],
		bank_mobile: [{
				required: true,
				message: '请输入预留手机号',
				trigger: 'blur,change'
			},
			{
				pattern: /^1[0-9]{10}$/g,
				// 正则检验前先将值转为字符串
				transform(value) {
					return String(value);
				},
				message: '请输入正确的手机号'
			},
		],
	},
	bankList:[],
	flag:true
}
const mutations = {
	//旧密码
	CHANGE_OLD_PASSWORD(state,data){
     state.transaction.old_pwd=data
	},
	//新密码
    CHANGE_NEW_PASSWORD(state,data){
		state.transaction.new_pwd=data
	}

}
const actions = {
	//债务人修改密码
	CRYPTOGRAM({
		state
	}) {
		personal.changePwd(state.values).then(res => {
			if (res) {
				state.values = JSON.parse(JSON.stringify(values)),
					toast.successTab('修改密码成功', '/pages/loanee/personal/index')
			}
		})
	},
	//债权人修改密码
	CREDITOR_TOGRAM({state}){
		personal.changePwd(state.values).then(res => {
			if (res) {
				state.values = JSON.parse(JSON.stringify(values)),
					toast.successTab('修改密码成功', '/pages/creditor/personal/index')
			}
		})
	},
	//获取银行列表
	GET_BANK_DATA({state}){
		login.bankListGet().then(res=>{
			if(res){
				state.flag=false
				res.data.map(item => {
					state.bankList.push({
						label: item,
						value: item
					})
				})
			}
		})
	},
	GET_BANK_INFO({state}){
		personal.bankInfo().then(res=>{
			if(res.data.bank_name){
				state.flag=true
				let info = state.card.filter(item => {
					return res.data.bank_name == item.name
				})
				state.bank = info[0]
				res.data.bank_card = date.cardFilter(res.data.bank_card)
				state.bank.bank_card = res.data.bank_card
			}else{
				state.flag=false
			}
		})
	},
	CONFIRM_DEBIT({state,dispatch}){
		personal.changeBankInfo(state.bankValues).then(res=>{
			if(res){
				state.flag=true
			 state.bankValues=JSON.parse(JSON.stringify(bankValues))
			 toast.success('更改成功,等待管理员审核' )
			 state.bankList=[]
			 dispatch('GET_BANK_INFO')
			}
		})
	},
	//帮助中心跟诉讼保障
	GET_HELP_LIST({state},data){
		personal.helpCenterPost({type:data}).then(res=>{
			if(res){
          state.litigation=res.data
			}
		})
	},
	//修改交易密码债务人
	CHANGE_CONFIRM_PASSWORD({state}){
	return 	personal.changePayPwd(state.transaction)
	},
	//修改交易密码债权人
	TRANSACTION_PASSWORD({state}){
	return	personal.changePayPwd(state.transaction)
	},
	
}
export default {
	namespaced: true,
	state,
	mutations,
	actions
}
